/*
 * XSplashScreen.java, created on Oct 20, 2005
 * 
 * Copyright (c) 2005 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.dialog;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xfc.components.XMultilineLabel;
import org.xfc.util.XUtils;

import com.jgoodies.forms.layout.FormLayout;

// TODO: add the ability to have a progress bar?

// TODO: progress text?


/**
 *
 *
 * @author Devon Carew
 */
public class XSplashScreen
	extends JWindow
{
	private static Log log = LogFactory.getLog(XSplashScreen.class);
	
	private JLabel 			imageLabel = new JLabel();
	private XMultilineLabel textLabel = new XMultilineLabel();
	
	private long 			shownAtMillis;
	
	public XSplashScreen(ImageIcon image)
	{
		setImage(image);
		
		getContentPane().setLayout(new FormLayout(
			"12 pref:grow 12",
			"12 pref:grow 12"
		));
		
		getContentPane().add(textLabel, 	"2, 2, f, b");
		getContentPane().add(imageLabel, 	"1, 1, 3, 3, f, f");
		
		textLabel.setFont(textLabel.getFont().deriveFont(Font.BOLD));
		textLabel.setForeground(Color.BLACK);
	}
	
	public Icon getImage()
	{
		return imageLabel.getIcon();
	}
	
	public void setImage(Icon image)
	{
		imageLabel.setIcon(image);
	}
	
	public String getText()
	{
		return textLabel.getText();
	}
	
	public JLabel getTextLabel()
	{
		return textLabel;
	}
	
	public void setForeground(Color color)
	{
		super.setForeground(color);
		
		textLabel.setForeground(color);
	}
	
	// TODO: and the border color -
	
	public void setText(String text)
	{
		textLabel.setText(text);
	}
	
	public void setHasBorder(boolean hasBorder)
	{
		imageLabel.setBorder(hasBorder ? BorderFactory.createLineBorder(Color.BLACK) : null);
	}

	public void addNotify()
	{
		setSize(getPreferredSize());
		
		XUtils.centerWindow(this);
		
		shownAtMillis = System.currentTimeMillis();
		
		super.addNotify();
	}
	
	/**
	 * Wait until the splash screen has been shown for the given number of milliseconds.
	 * This should not be called from the event thread, so that we can safely block the thread.
	 * 
	 * @param millis
	 */
	public void waitUntilShownFor(long millis)
	{
		// TODO: use FoxTrot to recover from this situation? Or just ignore it as it isn't
		// that critical?
		
		if (SwingUtilities.isEventDispatchThread())
			log.warn("waitUntilShownFor() should not be called from the event thread.");
		
		long remainingMillis = shownAtMillis + millis - System.currentTimeMillis(); 
		
		while (remainingMillis > 0)
		{
			try { Thread.sleep(remainingMillis); }
			catch (InterruptedException ie) { }
			
			remainingMillis = shownAtMillis + millis - System.currentTimeMillis();
		}
	}
	
}
